<%@page import="entity.Product"%>
<%@page import="entity.SaleOrderDetail"%>
<%@page import="service.SaleOrderDetailServiceLocal"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="entity.MemberCustomer"%>
<%@page import="java.util.Iterator"%>
<%@page import="entity.SaleOrder"%>
<%@page import="java.util.List"%>
<%@page import="service.SaleOrderServiceLocal"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<!--  start nav-outer-repeat................................................................................................. START -->
<div class="nav-outer-repeat"> 
<!--  start nav-outer -->
<div class="nav-outer"> 

		<!-- start nav-right -->
		<div id="nav-right">
                        <div class="showhide-account">&nbsp;</div>
			<div class="nav-divider">&nbsp;</div>
			<a href="<c:url value="/admin/login.jsp" />" id="logout"><img src="<c:url value="/layout/default/shared/nav/nav_logout.gif" />" width="64" height="14" alt="" /></a>
			<div class="clear">&nbsp;</div>
		
		</div>
		<!-- end nav-right -->


		<!--  start nav -->
		<div class="nav">
		<div class="table">
		
		                    
		<ul class="select"><li><a href="<c:url value="/admin/view_product.jsp" />"><b>Sản Phẩm</b><!--[if IE 7]><!--></a><!--<![endif]-->
		<!--[if lte IE 6]><table><tr><td><![endif]-->
		<div class="select_sub">
			<ul class="sub">
				<li><a href="<c:url value="/admin/view_product.jsp" />">Xem Sản Phẩm</a></li>
				<li><a href="<c:url value="/admin/update_product.jsp" />">Cập Nhật Sản Phẩm</a></li>
				<li><a href="<c:url value="/admin/add_product.jsp" />">Thêm Sản Phẩm</a></li>
			</ul>
		</div>
		<!--[if lte IE 6]></td></tr></table></a><![endif]-->
		</li>
		</ul>
		
		<div class="nav-divider">&nbsp;</div>
		
		<ul class="select"><li><a href="<c:url value="/admin/add_category.jsp" />"><b>Loại Sản Phẩm</b><!--[if IE 7]><!--></a><!--<![endif]-->
		<!--[if lte IE 6]><table><tr><td><![endif]-->
		<div class="select_sub">
			<ul class="sub">
				<li><a href="<c:url value="/admin/add_category.jsp" />">Thêm Loại Sản Phẩm</a></li>
			</ul>
		</div>
		<!--[if lte IE 6]></td></tr></table></a><![endif]-->
		</li>
		</ul>
		
		<div class="nav-divider">&nbsp;</div>
		
		<ul class="current"><li><a href="<c:url value="/admin/update_order.jsp" />"><b>Đơn Hàng</b><!--[if IE 7]><!--></a><!--<![endif]-->
		<!--[if lte IE 6]><table><tr><td><![endif]-->
		<div class="select_sub show">
			<ul class="sub">
				<li class="sub_show"><a href="<c:url value="/admin/update_order.jsp" />">Cập Nhật Trạng Thái Đơn Hàng</a></li>
			 
			</ul>
		</div>
		<!--[if lte IE 6]></td></tr></table></a><![endif]-->
		</li>
		</ul>		
		
		<div class="clear"></div>
		</div>
		<div class="clear"></div>
		</div>
		<!--  start nav -->

</div>
<div class="clear"></div>
<!--  start nav-outer -->
</div>
<!--  start nav-outer-repeat................................................... END -->

 <div class="clear"></div>
 
<!-- start content-outer ........................................................................................................................START -->
<div id="content-outer">
<!-- start content -->
<div id="content">

	<!--  start page-heading -->
	<div id="page-heading">
		<h1>Cập Nhật Trạng Thái Đơn Hàng</h1>
	</div>
	<!-- end page-heading -->
	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img src="<c:url value="/layout/default/shared/side_shadowleft.jpg" />" width="20" height="300" alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img src="<c:url value="/layout/default/shared/side_shadowright.jpg" />" width="20" height="300" alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td>
		<!--  start content-table-inner ...................................................................... START -->
		<div id="content-table-inner">
                    
			<!--  start table-content  -->
			<div id="table-content">
<% 
    String oid_str = "Mã Đơn Hàng";
    if(request.getAttribute("oid_str") != null) {
        oid_str = request.getAttribute("oid_str").toString();
    }
    String status_str = "0";
    if(request.getAttribute("status_str") != null) {
        status_str = request.getAttribute("status_str").toString();
    }    
%>                            
                            <table border="0"><tr>
                                    <td><form class="search-wrapper" method="post" action="<c:url value="/admin/search_order.do" />">
			<input type="text" class="search-field" value="<%=oid_str%>" name="oid" />
			<input type="submit" class="search-button dark" value="Tìm Kiếm" />
                    </form></td>
                    <td rowspan="2" align="center" valign="center" style="padding-left: 30px;">  
                        <form method="post" action="<c:url value="/admin/search_order.do" />">
                                   <fieldset style="border: 1px solid lightgrey;float:left;
	width: 250px;margin-bottom: 30px;">
<legend style="font-size:13px;
	font-weight:bold;
	padding-bottom: 10px;">Trạng Thái</legend>
<% 
String choice_1 = "";
String choice_2 = "";
String choice_3 = "";
String choice_4 = "";
String choice_5 = "";
if(status_str.equals("0")) {
    choice_1 = "selected='true'";
} else if(status_str.equals("Mới Đặt")) {
    choice_2 = "selected='true'";
} else if(status_str.equals("Chờ Giao Hàng")) {
    choice_3 = "selected='true'";
} else if(status_str.equals("Đã Giao Hàng")) {
    choice_4 = "selected='true'";
} else if(status_str.equals("Hủy")) {
    choice_5 = "selected='true'";
}
%>        
                                       <table border="0" style="margin-left: 10px; margin-bottom: 10px;"><tr><td>
                           <select name="status_search" class="styledselect_pages">                           
				<option value="0" <%=choice_1%> >-- chọn --</option>
                                <option value='Mới Đặt' <%=choice_2%> >Mới Đặt</option>
                                <option value='Chờ Giao Hàng' <%=choice_3%> >Chờ Giao Hàng</option>
                                <option value='Đã Giao Hàng' <%=choice_4%> >Đã Giao Hàng</option>
                                <option value='Hủy' <%=choice_5%> >Hủy</option>                             
                            </select></td>
                            <td><input type="submit" class="button lightText small regular black" value='Tìm Kiếm' /></td>
                                           </tr></table></fieldset>
                    </form></td> </tr>                           
</table>
                            
<% 
  SaleOrderServiceLocal saleOrderServiceLocal = (SaleOrderServiceLocal) application.getAttribute("saleOrderServiceLocal");
    List lstSaleOrders = saleOrderServiceLocal.getAllSaleOrders();
    if(request.getAttribute("saleorders_search") != null) {
        lstSaleOrders = (List<SaleOrder>) request.getAttribute("saleorders_search");
    }
    if(lstSaleOrders.size() > 0) {
%>                                
				<table border="1" style="border: 1px solid #d2d2d2;" width="100%" cellpadding="0" cellspacing="0" id="product-table">
				<tr bgcolor="#515151" style="color: #fff;
	font-family: Tahoma;
	font-size: 13px;
	font-weight: bold;
	line-height: 14px;">
					<th style="text-align: center;">Mã Đơn Hàng</th>
					<th style="text-align: center;">Khách Hàng</th>
					<th style="text-align: center;">Tên Khách Hàng</th>
					<th style="text-align: center;">Địa Chỉ Giao Hàng</th>
					<th style="text-align: center;">Số Điện Thoại</th>
                                        <th style="text-align: center;">Ngày Đặt Hàng</th>
                                        <th style="text-align: center;">Tổng Tiền (đ)</th>
                                        <th style="text-align: center;">Hình Thức Thanh Toán</th>
                                        <th style="text-align: center;">Trạng Thái</th>
					<th style="text-align: center;width: 50px;">-</th>
				</tr>
<%
    Iterator saleorders = lstSaleOrders.iterator();
    int j = 0;
    while(saleorders.hasNext()) {
        SaleOrder saleorder = (SaleOrder) saleorders.next();
        String color = ++j % 2 == 0 ? "alternate-row" : "";
        int oid = saleorder.getSaleOrderID();
        MemberCustomer member = saleorder.getMemberID();
        String member_str = "Thường";
        if(member != null) {
            member_str = "Thành Viên";
        }
        String customername = saleorder.getCustomerName();
        String address = saleorder.getShippingAddress();
        String phone = saleorder.getShippingPhone();
        String orderdate = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(saleorder.getOrderDate());
        BigDecimal totalprice = saleorder.getTotalPrice();
        String paymenttype = "Tiền Mặt Trực Tiếp";
        if(saleorder.getPaymentType().equals("Credit Card")) {
            paymenttype = "Credit Card";
        }
        String status = saleorder.getStatus();
%>            
<form id="mainform" method="post" action="<c:url value="/admin/update_status.do" />">
				<tr class="<%=color%>">
					<td style="text-align: center;"><%=oid%></td>
					<td style="text-align: center;padding-right: 5px;"><%=member_str%></td>
					<td style="text-align: center;padding-right: 5px;"><%=customername%></td>
					<td style="text-align: center;padding-right: 5px;"><%=address%></td>
					<td style="text-align: center;padding-right: 5px;"><%=phone%></td>
                                        <td style="text-align: center;"><%=orderdate%></td>
                                        <td style="text-align: center;padding-right: 5px;"><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=totalprice%>"/></td>
                                        <td style="text-align: center;padding-right: 5px;"><%=paymenttype%></td>
<% 
String choice_table_1 = "";
String choice_table_2 = "";
String choice_table_3 = "";
String choice_table_4 = "";
if(status.equals("Mới Đặt")) {
    choice_table_1 = "selected='true'";
} else if(status.equals("Chờ Giao Hàng")) {
    choice_table_2 = "selected='true'";
} else if(status.equals("Đã Giao Hàng")) {
    choice_table_3 = "selected='true'";
} else if(status.equals("Hủy")) {
    choice_table_4 = "selected='true'";
}
%>                                         
                                        <td style="text-align: center;">
                                           <select name="status_update" class="styledselect_pages">                           
                                                <option value='Mới Đặt' <%=choice_table_1%> >Mới Đặt</option>
                                                <option value='Chờ Giao Hàng' <%=choice_table_2%> >Chờ Giao Hàng</option>
                                                <option value='Đã Giao Hàng' <%=choice_table_3%> >Đã Giao Hàng</option>
                                                <option value='Hủy' <%=choice_table_4%> >Hủy</option>                             
                                            </select>                                            
                                        </td>
					<td style="text-align: center;padding-right: 10px;">
                                            <input type="hidden" name="oid" value="<%=oid%>"/>
					<INPUT TYPE="image" SRC="<c:url value="/images/update.png" />" HEIGHT="24" WIDTH="24" title="Cập Nhật" BORDER="0" 
                           ALT="Submit Form">
					</td>
				</tr>
                                <tr class="<%=color%>">
                                    <td colspan="10" style="padding-right: 20px;padding-bottom: 30px;padding-left: 20px;">
				<table border="1" style="border: 1px solid #d2d2d2;" width="100%" cellpadding="0" cellspacing="0" id="product-table">
				<tr bgcolor="#8ac658" style="color: #fff; padding-left: 0px; padding-right: 0px;
	font-family: Tahoma;
	font-size: 13px;
	font-weight: bold;
	line-height: 14px;">
					<th style="text-align: center;">#</th>
					<th style="text-align: center;">Mã Sản Phẩm</th>
					<th style="text-align: center;">Tên Sản Phẩm</th>
                                        <th style="text-align: center;">Đơn Giá (đ)</th>
					<th style="text-align: center;">Số Lượng Mua</th>                                        
                                        <th style="text-align: center;">Thành Tiền (đ)</th>
				</tr>
<% 
    SaleOrderDetailServiceLocal saleOrderDetailServiceLocal = (SaleOrderDetailServiceLocal) application.getAttribute("saleOrderDetailServiceLocal");
    List<SaleOrderDetail> lstSaleOrderDetails = saleOrderDetailServiceLocal.getAllSaleOrderDetails_bySaleOrderID(oid);
    int k = 0;
    for(int m = 0; m < lstSaleOrderDetails.size(); m++) {
        String color_table2 = ++k % 2 == 0 ? "#e0f2cb" : "#f5f9f1";
        Product product = lstSaleOrderDetails.get(m).getProductID();
        String pid = product.getProductID();
        int qty = lstSaleOrderDetails.get(m).getOrderQty();
        BigDecimal totallinefmt = lstSaleOrderDetails.get(m).getLineTotal();
        String pname = product.getProductName();
        BigDecimal unitpricefmt = product.getUnitPrice();       
%>                               
				<tr bgcolor="<%=color_table2%>">
					<td style="text-align: center;"><%=k%></td>
					<td style="text-align: center;"><%=pid%></td>
					<td style="text-align: center;"><%=pname%></td>
                                        <td style="text-align: center;"><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=unitpricefmt%>"/></td>                                        
					<td style="text-align: center;"><%=qty%></td>
                                        <td style="text-align: center;"><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=totallinefmt%>"/></td> 
				</tr>
<% } %>
				</table> <!--  end product-table................................... -->                                         
                                    </td>
                                </tr>                     
</form>
<% } %>
				</table> <!--  end product-table................................... --> 
<% } else { %>                                
<p style="font-weight:bold;">Không tìm thấy bất kỳ Đơn Hàng nào!</p>		
<% } %>
                        
			</div>
			<!--  end content-table  -->				
			
			<div class="clear"></div>
		 
		</div>
		<!--  end content-table-inner ............................................END  -->
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	<tr>
		<th class="sized bottomleft"></th>
		<td id="tbl-border-bottom">&nbsp;</td>
		<th class="sized bottomright"></th>
	</tr>
	</table>
	<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer........................................................END -->
<% 
if(((HttpServletRequest) request).getAttribute("notify") != null) {
    String notify = ((HttpServletRequest) request).getAttribute("notify").toString();
    if(notify.length() > 0) { 
        out.println("<script type=\"text/javascript\">");
        out.println("alert(\"" + notify + "\")");
        out.println("</script>");
    }
} %>